package eteam.aps.controller;

import eteam.aps.common.R;
import eteam.aps.common.exceptions.BusinessException;
import eteam.aps.common.helper.Helper;
import eteam.aps.common.helper.JsonPage;
import eteam.aps.model.dto.WorkshopSectionQueryDto;
import eteam.aps.model.vo.workshop_section.WorkshopSectionShowVo;
import eteam.aps.model.vo.workshop_section.WorkshopSectionUpdateVo;
import eteam.aps.model.vo.workshop_section.WorkshopSectionVo;
import eteam.aps.service.WorkshopSectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

/**
 * 工段信息 控制层;
 *
 * @author LiuLei
 */
@Api(tags = "工段信息")
@RestController
@RequestMapping("/wsc")
public class WorkshopSectionController {

    @Autowired
    private WorkshopSectionService workshopSectionService;

    @PostMapping("/jsp")
    @ApiOperation("分页查询工段信息")
    @ApiImplicitParam(name = "jsonPage<WorkshopSectionShowVo>", value = "查询条件")
    public R<JsonPage<WorkshopSectionShowVo>> selWSJsp(@RequestBody WorkshopSectionQueryDto wsq) throws Exception {
        JsonPage<WorkshopSectionShowVo> jsonPage = workshopSectionService.selWSJsp(wsq);
        if (jsonPage.getTotalCount() == 0) throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无数据！");
        return R.success(jsonPage);
    }

    @PostMapping("/simjsp")
    @ApiOperation("分页查询工段信息（工序）")
    @ApiImplicitParam(name = "jsonPage<WorkshopSectionVo>", value = "查询条件")
    public R<JsonPage<WorkshopSectionVo>> selWSSimJsp(@RequestBody WorkshopSectionQueryDto wsq) {
        JsonPage<WorkshopSectionVo> jsonPage = workshopSectionService.selWSSimJsp(wsq);
        if (jsonPage.getTotalCount() == 0) throw new BusinessException(HttpStatus.BAD_REQUEST, "暂无数据！");
        return R.success(jsonPage);
    }

    @PostMapping("/save")
    @ApiOperation("增加工段信息")
    @ApiImplicitParam(name = "WorkshopSectionUpdateVo", value = "新增条件")
    public R<Integer> saveWS(@RequestBody WorkshopSectionUpdateVo wsu) throws Exception {
        if (Helper.isEmpty(wsu.getfCode())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工段编号不能为空!");
        if (Helper.isEmpty(wsu.getfName())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工段名称不能为空!");
        int count = workshopSectionService.selByNameAndCode(wsu.getfCode(), wsu.getfName());
        if (count != 0) throw new BusinessException(HttpStatus.BAD_REQUEST, "已存在工序信息");
        int save = workshopSectionService.saveWS(wsu);
        return R.success(save);
    }

    @PostMapping("/del/{fId}")
    @ApiOperation("删除工段信息")
    @ApiImplicitParam(name = "fId", value = "内码")
    public R<Integer> delWS(@PathVariable String fId) throws Exception {
        if (Helper.isEmpty(fId)) throw new BusinessException(HttpStatus.BAD_REQUEST, "fId 不能为空!");
        int del = workshopSectionService.delWS(fId);
        return R.success(del);
    }

    @PostMapping("/update")
    @ApiOperation("修改工段信息")
    @ApiImplicitParam(name = "WorkshopSectionUpdateVo", value = "修改条件")
    public R<Integer> updateWS(@RequestBody WorkshopSectionUpdateVo wsu) throws Exception {
        if (Helper.isEmpty(wsu.getfId())) throw new BusinessException(HttpStatus.BAD_REQUEST, "内码不能为空!");
        if (Helper.isEmpty(wsu.getfCode())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工段编号不能为空!");
        if (Helper.isEmpty(wsu.getfName())) throw new BusinessException(HttpStatus.BAD_REQUEST, "工段名称不能为空!");
        int update = workshopSectionService.updateWS(wsu);
        return R.success(update);
    }

}